<template>
  <div class="card-container">
    <el-form :model="form" ref="formRef" label-width="80px" style="width: 100%" :rules="formRules">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>基本信息</span>
        </div>
        <el-form-item label="分类：" prop="pid">
          <el-select v-model="form.pid" placeholder="请选择站点分类">
            <el-option v-for="item in cateList" :key="item.id" :label="item.title" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称：" prop="title">
          <el-input v-model="form.title" placeholder="请输入站点名称"></el-input>
        </el-form-item>
        <el-form-item label="简介：" prop="desc">
          <el-input type="textarea" :rows="2" v-model="form.desc" placeholder="请输入站点简介"></el-input>
        </el-form-item>
        <el-form-item label="链接：" prop="url">
          <el-input v-model="form.url" placeholder="请输入站点链接"></el-input>
          <div class="tips">不需要填写http或https</div>
        </el-form-item>
        <el-form-item label="Logo：" prop="logo">
          <upload-img v-model="form.logo" @click="showImgManager" @remove="removeImg" index="1"></upload-img>
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input v-model.number="form.sort" placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item label="添加推荐：" prop="is_recommend">
          <div class="radio-box">
            <el-radio v-model="form.is_recommend" label="1">是</el-radio>
            <el-radio v-model="form.is_recommend" label="0">否</el-radio>
          </div>
        </el-form-item>
        <el-form-item label="启用：" prop="is_show">
          <el-switch v-model="form.is_show" active-value="1" inactive-value="0" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
        </el-form-item>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <img-manager @change="checkedImg" ref="imgManager"></img-manager>
  </div>
</template>

<script>
import { addSite } from '@/api/site'
import { getSimpleCateList } from '@/api/category'
export default {
  data() {
    return {
      cateList: [],
      form: {
        pid: '',
        title: '',
        desc: '',
        logo: '',
        url: '',
        sort: '1',
        is_show: '1',
        is_recommend: '0',
      },
      formRules: {
        pid: [{ required: true, message: '请选择站点分类', trigger: 'change' }],
        title: [{ required: true, message: '请输入站点标题', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入站点描述', trigger: 'blur' }],
        logo: [{ required: true, message: '请上传站点logo', trigger: 'change' }],
        url: [{ required: true, message: '请输入站点链接', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入分类排序', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const res = await getSimpleCateList()
      this.cateList = res.data
    },
    showImgManager(index) {
      this.$refs.imgManager.showDialog(index)
    },
    checkedImg(pic, index) {
      this.form.logo = pic
      this.$refs.formRef.validateField('logo')
    },
    removeImg(index) {
      this.form.logo = ''
    },
    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        const res = await addSite(this.form)
        if (res.code != 1) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        this.closePage()
      })
      console.log(this.form)
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/siteList')
    },
    resetForm() {
      this.form = this.$options.data().form
    },
  },
  computed: {},
}
</script>

<style lang="scss" scoped>
.add-cate-container {
  height: 700px;
}
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  line-height: 36px;
}
.el-form-item {
  width: 420px;
}
.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
.tips {
  margin-top: 2px;
  color: #999;
  font-size: 12px;
}
.radio-box {
  display: flex;
  align-items: center;
  height: 36px;
  line-height: 36px;
}
</style>
